﻿using System;
using System.Text;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using Microsoft.VisualBasic;

namespace 文件管家
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string selectPath;    //datagridview 中选中的文件路径
        public static int selectRowsIndex;  //datagridview 中rows 的下标
        public static string fileType;      //datagridview 中选中的文件类型

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void iconPictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label9_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                label8.Text = openFileDialog.SelectedPath;
                file.getFileChild(openFileDialog.SelectedPath,dataGridView1);
            }
            
        }

        private void dataGridView1_SelectIndexChange(object sender, int index)
        {
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Console.WriteLine(dataGridView1.CurrentRow.Cells[e.RowIndex].Value);    //dataGridView1.CurrentCell.Value
            selectRowsIndex = e.RowIndex;
            selectPath = dataGridView1.Rows[selectRowsIndex].Cells[0].Value.ToString();
            fileType = dataGridView1.Rows[selectRowsIndex].Cells[2].Value.ToString();
        }

        private void label10_Click(object sender, EventArgs e)
        {
            //新建
            file.creatingFolder(selectPath);
        }

        private void label12_Click(object sender, EventArgs e)
        {
            file.readFile(selectPath);
        }

        private void label11_Click(object sender, EventArgs e)
        {
            file.removeFile(selectPath,fileType);
        }

        private void label4_Click(object sender, EventArgs e)
        {
            
        }

        private void label14_Click(object sender, EventArgs e)
        {
            //清理temp
            string path = @"C:\Temp\";
            Directory.Delete(path, true);
        }

        private void label5_Click(object sender, EventArgs e)
        {
            panel10.Visible = false;
            panel3.Controls.Clear();
            panel15.Visible = true;
            panel15.Margin = new System.Windows.Forms.Padding(3, 3, 3, 3);
            panel3.Controls.Add(panel15);
        }
    }
    class file
    {
        private static string filePath;
        // 获取文件夹里面的文件和文件夹
        public static void getFileChild(string path,DataGridView control)
        {
            filePath = path;
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            DirectoryInfo[] directoryInfos =  directoryInfo.GetDirectories();
            foreach (DirectoryInfo directoryInfo1 in directoryInfos)
            {
                control.Rows.Add(directoryInfo1.FullName,directoryInfo1.Name,"文件夹");
            }
            FileInfo[] fileInfos = directoryInfo.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
            {
                control.Rows.Add(fileInfo.FullName,fileInfo.Name, fileInfo.Name.Split('.')[1]);
            }
        }
        //创建文件夹
        public static void creatingFolder(string folderPath)
        {
            try
            {
                string folderName = Interaction.InputBox("请输入文件夹名", "文件夹名", "输入");
                Directory.CreateDirectory(folderPath + @"\" + folderName);
                MessageBox.Show(folderPath + @"\" + folderName + "   创建成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.ToString());
            }
        }
        // 创建文件
        public static void readFile(string filePath)
        {
            try
            {
                string fileCount = File.ReadAllText(filePath, Encoding.Default);
                MessageBox.Show("操作成功");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.ToString());
            }
        }
        // 删除文件和文件夹
        public static void removeFile(string path,string filetype)
        {
            try
            {
                if (File.Exists(path))
                {
                    if (filetype == "文件夹")
                    {
                        Directory.Delete(path, true);

                    }
                    else
                    {
                        File.Delete(path);
                    }
                }
                MessageBox.Show("操作成功");
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.ToString());

            }

        }
    }
}
